

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="https://image.lxandzjy.top/2022/06/06/8871b9e332dbe.png">
  <link rel="icon" href="https://image.lxandzjy.top/2022/06/06/8871b9e332dbe.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="zjy">
  <meta name="keywords" content="学习笔记,vue,axios,,JavaScript,前端,后端">
  
    <meta name="description" content="1.1 初始化项目新建一个目录，初始化 npm 1npm init  webpack 是运行在 node 环境中的,我们需要安装以下两个 npm 包 1npm i -D webpack webpack-cli   npm i -D 为 npm install –save-dev 的缩写 npm i -S 为 npm install –save 的缩写  新建一个文件夹src ,然后新建一个文件ma">
<meta property="og:type" content="article">
<meta property="og:title" content="webpack实战">
<meta property="og:url" content="https://zjy365.github.io/2021/11/10/webpack%E5%AE%9E%E6%88%98/index.html">
<meta property="og:site_name" content="土猪的个人博客">
<meta property="og:description" content="1.1 初始化项目新建一个目录，初始化 npm 1npm init  webpack 是运行在 node 环境中的,我们需要安装以下两个 npm 包 1npm i -D webpack webpack-cli   npm i -D 为 npm install –save-dev 的缩写 npm i -S 为 npm install –save 的缩写  新建一个文件夹src ,然后新建一个文件ma">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://image.lxandzjy.top/2021/11/17/0e1611dd10763.png">
<meta property="og:image" content="https://image.lxandzjy.top/2021/11/17/55834c91a5e45.png">
<meta property="og:image" content="https://image.lxandzjy.top/2021/11/17/eccbd354b1c97.png">
<meta property="article:published_time" content="2021-11-10T12:46:25.000Z">
<meta property="article:modified_time" content="2022-06-06T13:44:45.816Z">
<meta property="article:author" content="zjy">
<meta property="article:tag" content="webpack">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://image.lxandzjy.top/2021/11/17/0e1611dd10763.png">
  
  
  
  <title>webpack实战 - 土猪的个人博客</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"zjy365.github.io","root":"/","version":"1.9.1","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>

  
<meta name="generator" content="Hexo 5.4.2"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>土猪</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('https://image.lxandzjy.top/2022/06/06/aea923ea1a0e8.jpg') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="webpack实战"></span>
          
        </div>

        
          
  <div class="mt-3">
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2021-11-10 20:46" pubdate>
          2021年11月10日 晚上
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          16k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          133 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">webpack实战</h1>
            
            <div class="markdown-body">
              
              <h4 id="1-1-初始化项目"><a href="#1-1-初始化项目" class="headerlink" title="1.1 初始化项目"></a>1.1 初始化项目</h4><p>新建一个目录，初始化 npm</p>
<figure class="highlight coffeescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs coffeescript"><span class="hljs-built_in">npm</span> init<br></code></pre></td></tr></table></figure>

<p>webpack 是运行在 node 环境中的,我们需要安装以下两个 npm 包</p>
<figure class="highlight avrasm"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs avrasm">npm i -D webpack webpack-<span class="hljs-keyword">cli</span><br></code></pre></td></tr></table></figure>

<ul>
<li>npm i -D 为 npm install –save-dev 的缩写</li>
<li>npm i -S 为 npm install –save 的缩写</li>
</ul>
<p>新建一个文件夹<code>src</code> ,然后新建一个文件<code>main.js</code>,写一点代码测试一下</p>
<p>​ <img src="https://image.lxandzjy.top/2021/11/17/0e1611dd10763.png" srcset="/img/loading.gif" lazyload></p>
<h3 id="1-2-开始我们自己的配置"><a href="#1-2-开始我们自己的配置" class="headerlink" title="1.2 开始我们自己的配置"></a>1.2 开始我们自己的配置</h3><p>上面一个简单的例子只是 webpack 自己默认的配置，下面我们要实现更加丰富的自定义配置<br>新建一个<code>build</code>文件夹,里面新建一个<code>webpack.config.js</code></p>
<figure class="highlight awk"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs awk"><span class="hljs-regexp">//</span> webpack.config.js<br><br>const path = require(<span class="hljs-string">&#x27;path&#x27;</span>);<br>module.exports = &#123;<br>    mode:<span class="hljs-string">&#x27;development&#x27;</span>, <span class="hljs-regexp">//</span> 开发模式<br>    entry: path.resolve(__dirname,<span class="hljs-string">&#x27;../src/main.js&#x27;</span>),    <span class="hljs-regexp">//</span> 入口文件<br>    output: &#123;<br>        filename: <span class="hljs-string">&#x27;output.js&#x27;</span>,      <span class="hljs-regexp">//</span> 打包后的文件名称<br>        path: path.resolve(__dirname,<span class="hljs-string">&#x27;../dist&#x27;</span>)  <span class="hljs-regexp">//</span> 打包后的目录<br>    &#125;<br>&#125;<br><br></code></pre></td></tr></table></figure>

<h3 id="1-3-配置-html-模板"><a href="#1-3-配置-html-模板" class="headerlink" title="1.3 配置 html 模板"></a>1.3 配置 html 模板</h3><p>js 文件打包好了,但是我们不可能每次在<code>html</code>文件中手动引入打包好的 js</p>
<blockquote>
<p>这里可能有的朋友会认为我们打包 js 文件名称不是一直是固定的嘛(output.js)？这样每次就不用改动引入文件名称了呀？实际上我们日常开发中往往会这样配置:</p>
</blockquote>
<figure class="highlight awk"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs awk">module.exports = &#123;<br>    <span class="hljs-regexp">//</span> 省略其他配置<br>    output: &#123;<br>      filename: <span class="hljs-string">&#x27;[name].[hash:8].js&#x27;</span>,      <span class="hljs-regexp">//</span> 打包后的文件名称<br>      path: path.resolve(__dirname,<span class="hljs-string">&#x27;../dist&#x27;</span>)  <span class="hljs-regexp">//</span> 打包后的目录<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>为了缓存，你会发现打包好的 js 文件的名称每次都不一样。webpack 打包出来的 js 文件我们需要引入到 html 中，但是每次我们都手动修改 js 文件名显得很麻烦，因此我们需要一个插件来帮我们完成这件事情</p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs css">npm <span class="hljs-selector-tag">i</span> -D <span class="hljs-selector-tag">html</span>-webpack-plugin<br></code></pre></td></tr></table></figure>

<p>新建一个<code>build</code>同级的文件夹<code>public</code>,里面新建一个 index.html<br>具体配置文件如下</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-comment">// webpack.config.js</span><br><span class="hljs-keyword">const</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;path&#x27;</span>);<br><span class="hljs-keyword">const</span> <span class="hljs-title class_">HtmlWebpackPlugin</span> = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;html-webpack-plugin&#x27;</span>)<br><span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span> = &#123;<br>    <span class="hljs-attr">mode</span>:<span class="hljs-string">&#x27;development&#x27;</span>, <span class="hljs-comment">// 开发模式</span><br>    <span class="hljs-attr">entry</span>: path.<span class="hljs-title function_">resolve</span>(__dirname,<span class="hljs-string">&#x27;../src/main.js&#x27;</span>),    <span class="hljs-comment">// 入口文件</span><br>    <span class="hljs-attr">output</span>: &#123;<br>      <span class="hljs-attr">filename</span>: <span class="hljs-string">&#x27;[name].[hash:8].js&#x27;</span>,      <span class="hljs-comment">// 打包后的文件名称</span><br>      <span class="hljs-attr">path</span>: path.<span class="hljs-title function_">resolve</span>(__dirname,<span class="hljs-string">&#x27;../dist&#x27;</span>)  <span class="hljs-comment">// 打包后的目录</span><br>    &#125;,<br>    <span class="hljs-attr">plugins</span>:[<br>      <span class="hljs-keyword">new</span> <span class="hljs-title class_">HtmlWebpackPlugin</span>(&#123;<br>        <span class="hljs-attr">template</span>:path.<span class="hljs-title function_">resolve</span>(__dirname,<span class="hljs-string">&#x27;../public/index.html&#x27;</span>)<br>      &#125;)<br>    ]<br>&#125;<br></code></pre></td></tr></table></figure>

<h4 id="1-3-1-多入口文件如何开发"><a href="#1-3-1-多入口文件如何开发" class="headerlink" title="1.3.1 多入口文件如何开发"></a>1.3.1 多入口文件如何开发</h4><blockquote>
<p>生成多个 html-webpack-plugin 实例来解决这个问题</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">const</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;path&#x27;</span>);<br><span class="hljs-keyword">const</span> <span class="hljs-title class_">HtmlWebpackPlugin</span> = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;html-webpack-plugin&#x27;</span>)<br><span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span> = &#123;<br>    <span class="hljs-attr">mode</span>:<span class="hljs-string">&#x27;development&#x27;</span>, <span class="hljs-comment">// 开发模式</span><br>    <span class="hljs-attr">entry</span>: &#123;<br>      <span class="hljs-attr">main</span>:path.<span class="hljs-title function_">resolve</span>(__dirname,<span class="hljs-string">&#x27;../src/main.js&#x27;</span>),<br>      <span class="hljs-attr">header</span>:path.<span class="hljs-title function_">resolve</span>(__dirname,<span class="hljs-string">&#x27;../src/header.js&#x27;</span>)<br>  &#125;,<br>    <span class="hljs-attr">output</span>: &#123;<br>      <span class="hljs-attr">filename</span>: <span class="hljs-string">&#x27;[name].[hash:8].js&#x27;</span>,      <span class="hljs-comment">// 打包后的文件名称</span><br>      <span class="hljs-attr">path</span>: path.<span class="hljs-title function_">resolve</span>(__dirname,<span class="hljs-string">&#x27;../dist&#x27;</span>)  <span class="hljs-comment">// 打包后的目录</span><br>    &#125;,<br>    <span class="hljs-attr">plugins</span>:[<br>      <span class="hljs-keyword">new</span> <span class="hljs-title class_">HtmlWebpackPlugin</span>(&#123;<br>        <span class="hljs-attr">template</span>:path.<span class="hljs-title function_">resolve</span>(__dirname,<span class="hljs-string">&#x27;../public/index.html&#x27;</span>),<br>        <span class="hljs-attr">filename</span>:<span class="hljs-string">&#x27;index.html&#x27;</span>,<br>        <span class="hljs-attr">chunks</span>:[<span class="hljs-string">&#x27;main&#x27;</span>] <span class="hljs-comment">// 与入口文件对应的模块名</span><br>      &#125;),<br>      <span class="hljs-keyword">new</span> <span class="hljs-title class_">HtmlWebpackPlugin</span>(&#123;<br>        <span class="hljs-attr">template</span>:path.<span class="hljs-title function_">resolve</span>(__dirname,<span class="hljs-string">&#x27;../public/header.html&#x27;</span>),<br>        <span class="hljs-attr">filename</span>:<span class="hljs-string">&#x27;header.html&#x27;</span>,<br>        <span class="hljs-attr">chunks</span>:[<span class="hljs-string">&#x27;header&#x27;</span>] <span class="hljs-comment">// 与入口文件对应的模块名</span><br>      &#125;),<br>    ]<br>&#125;<br></code></pre></td></tr></table></figure>

<h4 id="1-3-2-clean-webpack-plugin"><a href="#1-3-2-clean-webpack-plugin" class="headerlink" title="1.3.2 clean-webpack-plugin"></a>1.3.2 clean-webpack-plugin</h4><blockquote>
<p>每次执行 npm run build 会发现 dist 文件夹里会残留上次打包的文件，这里我们推荐一个 plugin 来帮我们在打包输出前清空文件夹<code>clean-webpack-plugin</code></p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">const</span> &#123;<span class="hljs-title class_">CleanWebpackPlugin</span>&#125; = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;clean-webpack-plugin&#x27;</span>)<br><span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span> = &#123;<br>    <span class="hljs-comment">// ...省略其他配置</span><br>    <span class="hljs-attr">plugins</span>:[<span class="hljs-keyword">new</span> <span class="hljs-title class_">CleanWebpackPlugin</span>()]<br>&#125;<br></code></pre></td></tr></table></figure>

<h3 id="1-4-引用-CSS"><a href="#1-4-引用-CSS" class="headerlink" title="1.4 引用 CSS"></a>1.4 引用 CSS</h3><p>我们的入口文件是 js，所以我们在入口 js 中引入我们的 css 文件</p>
<p><img src="https://image.lxandzjy.top/2021/11/17/55834c91a5e45.png" srcset="/img/loading.gif" lazyload></p>
<p>同时我们也需要一些 loader 来解析我们的 css 文件 scss 文件</p>
<figure class="highlight crmsh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs crmsh">npm install -D sass-loader <span class="hljs-keyword">node</span><span class="hljs-title">-sass</span> style-loader css-loader<br></code></pre></td></tr></table></figure>

<figure class="highlight awk"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><code class="hljs awk">&#123;<br>test:<span class="hljs-regexp">/\.css$/</span>,<br>use:[<span class="hljs-string">&#x27;style-loader&#x27;</span>,<span class="hljs-string">&#x27;css-loader&#x27;</span>] <span class="hljs-regexp">//</span> 从右向左解析原则<br>&#125;,<br>&#123;<br>    test: <span class="hljs-regexp">/\.scss$/</span>,<br>    use:[ <span class="hljs-string">&#x27;style-loader&#x27;</span>,<span class="hljs-string">&#x27;css-loader&#x27;</span>,<span class="hljs-string">&#x27;postcss-loader&#x27;</span>, <span class="hljs-string">&#x27;sass-loader&#x27;</span>],<br>&#125;,<br>&#123;<br>    test: <span class="hljs-regexp">/\.(jpe?g|png|gif)$/i</span>, <span class="hljs-regexp">//</span>图片文件<br>    use: [<br>      &#123;<br>        loader: <span class="hljs-string">&#x27;url-loader&#x27;</span>,<br>        options: &#123;<br>          limit: <span class="hljs-number">10240</span>,<br>          fallback: &#123;<br>            loader: <span class="hljs-string">&#x27;file-loader&#x27;</span>,<br>            options: &#123;<br>                name: <span class="hljs-string">&#x27;img/[name].[hash:8].[ext]&#x27;</span><br>            &#125;<br>          &#125;<br>        &#125;<br>      &#125;<br>    ]<br>&#125;,<br></code></pre></td></tr></table></figure>

<h3 id="1-5-打包-图片、字体、媒体、等文件"><a href="#1-5-打包-图片、字体、媒体、等文件" class="headerlink" title="1.5 打包 图片、字体、媒体、等文件"></a>1.5 打包 图片、字体、媒体、等文件</h3><p><code>file-loader</code>就是将文件在进行一些处理后（主要是处理文件名和路径、解析文件 url），并将文件移动到输出的目录中<br><code>url-loader</code> 一般与<code>file-loader</code>搭配使用，功能与 file-loader 类似，如果文件小于限制的大小。则会返回 base64 编码，否则使用 file-loader 将文件移动到输出的目录中</p>
<figure class="highlight nestedtext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><code class="hljs nestedtext"><span class="hljs-attribute">// webpack.config.js</span><br><span class="hljs-attribute">module.exports = &#123;</span><br><span class="hljs-attribute">  // 省略其它配置 ...</span><br><span class="hljs-attribute">  module</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&#123;</span><br>    <span class="hljs-attribute">rules</span><span class="hljs-punctuation">:</span> <span class="hljs-string">[</span><br>      <span class="hljs-attribute">// ...</span><br><span class="hljs-attribute">      &#123;</span><br><span class="hljs-attribute">        test</span><span class="hljs-punctuation">:</span> <span class="hljs-string">/\.(jpe?g|png|gif)$/i, //图片文件</span><br>        <span class="hljs-attribute">use</span><span class="hljs-punctuation">:</span> <span class="hljs-string">[</span><br>          <span class="hljs-attribute">&#123;</span><br><span class="hljs-attribute">            loader</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&#x27;url-loader&#x27;,</span><br>            <span class="hljs-attribute">options</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&#123;</span><br>              <span class="hljs-attribute">limit</span><span class="hljs-punctuation">:</span> <span class="hljs-string">10240,</span><br>              <span class="hljs-attribute">fallback</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&#123;</span><br>                <span class="hljs-attribute">loader</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&#x27;file-loader&#x27;,</span><br>                <span class="hljs-attribute">options</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&#123;</span><br>                    <span class="hljs-attribute">name</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&#x27;img/[name].[hash:8].[ext]&#x27;</span><br>                <span class="hljs-attribute">&#125;</span><br><span class="hljs-attribute">              &#125;</span><br><span class="hljs-attribute">            &#125;</span><br><span class="hljs-attribute">          &#125;</span><br><span class="hljs-attribute">        ]</span><br><span class="hljs-attribute">      &#125;,</span><br><span class="hljs-attribute">      &#123;</span><br><span class="hljs-attribute">        test</span><span class="hljs-punctuation">:</span> <span class="hljs-string">/\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/, //媒体文件</span><br>        <span class="hljs-attribute">use</span><span class="hljs-punctuation">:</span> <span class="hljs-string">[</span><br>          <span class="hljs-attribute">&#123;</span><br><span class="hljs-attribute">            loader</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&#x27;url-loader&#x27;,</span><br>            <span class="hljs-attribute">options</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&#123;</span><br>              <span class="hljs-attribute">limit</span><span class="hljs-punctuation">:</span> <span class="hljs-string">10240,</span><br>              <span class="hljs-attribute">fallback</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&#123;</span><br>                <span class="hljs-attribute">loader</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&#x27;file-loader&#x27;,</span><br>                <span class="hljs-attribute">options</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&#123;</span><br>                  <span class="hljs-attribute">name</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&#x27;media/[name].[hash:8].[ext]&#x27;</span><br>                <span class="hljs-attribute">&#125;</span><br><span class="hljs-attribute">              &#125;</span><br><span class="hljs-attribute">            &#125;</span><br><span class="hljs-attribute">          &#125;</span><br><span class="hljs-attribute">        ]</span><br><span class="hljs-attribute">      &#125;,</span><br><span class="hljs-attribute">      &#123;</span><br><span class="hljs-attribute">        test</span><span class="hljs-punctuation">:</span> <span class="hljs-string">/\.(woff2?|eot|ttf|otf)(\?.*)?$/i, // 字体</span><br>        <span class="hljs-attribute">use</span><span class="hljs-punctuation">:</span> <span class="hljs-string">[</span><br>          <span class="hljs-attribute">&#123;</span><br><span class="hljs-attribute">            loader</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&#x27;url-loader&#x27;,</span><br>            <span class="hljs-attribute">options</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&#123;</span><br>              <span class="hljs-attribute">limit</span><span class="hljs-punctuation">:</span> <span class="hljs-string">10240,</span><br>              <span class="hljs-attribute">fallback</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&#123;</span><br>                <span class="hljs-attribute">loader</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&#x27;file-loader&#x27;,</span><br>                <span class="hljs-attribute">options</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&#123;</span><br>                  <span class="hljs-attribute">name</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&#x27;fonts/[name].[hash:8].[ext]&#x27;</span><br>                &#125;<br>              &#125;<br>            &#125;<br>          &#125;<br>        ]<br>      &#125;,<br>    ]<br>  &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h3 id="1-6-用-babel-转义-js-文件"><a href="#1-6-用-babel-转义-js-文件" class="headerlink" title="1.6 用 babel 转义 js 文件"></a>1.6 用 babel 转义 js 文件</h3><p>为了使我们的 js 代码兼容更多的环境我们需要安装依赖</p>
<figure class="highlight coffeescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs coffeescript"><span class="hljs-built_in">npm</span> i -D babel-loader @babel/preset-env @babel/core<br></code></pre></td></tr></table></figure>

<ul>
<li>注意 <code>babel-loader</code>与<code>babel-core</code>的版本对应关系</li>
</ul>
<ol>
<li><code>babel-loader</code> 8.x 对应<code>babel-core</code> 7.x</li>
<li><code>babel-loader</code> 7.x 对应<code>babel-core</code> 6.x<br>配置如下</li>
</ol>
<figure class="highlight awk"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><code class="hljs awk"><span class="hljs-regexp">//</span> webpack.config.js<br>module.exports = &#123;<br>    <span class="hljs-regexp">//</span> 省略其它配置 ...<br>    module:&#123;<br>        rules:[<br>          &#123;<br>            test:<span class="hljs-regexp">/\.js$/</span>,<br>            use:&#123;<br>              loader:<span class="hljs-string">&#x27;babel-loader&#x27;</span>,<br>              options:&#123;<br>                presets:[<span class="hljs-string">&#x27;@babel/preset-env&#x27;</span>]<br>              &#125;<br>            &#125;,<br>            exclude:<span class="hljs-regexp">/node_modules/</span><br>          &#125;,<br>       ]<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>上面的<code>babel-loader</code>只会将 ES6/7/8 语法转换为 ES5 语法，但是对新 api 并不会转换 例如(promise、Generator、Set、Maps、Proxy 等)<br>此时我们需要借助 babel-polyfill 来帮助我们转换</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs java">npm i <span class="hljs-meta">@babel</span>/polyfill<br><br><span class="hljs-comment">// webpack.config.js</span><br><span class="hljs-type">const</span> <span class="hljs-variable">path</span> <span class="hljs-operator">=</span> require(<span class="hljs-string">&#x27;path&#x27;</span>)<br><span class="hljs-keyword">module</span>.<span class="hljs-keyword">exports</span> = &#123;<br>    entry: [<span class="hljs-string">&quot;@babel/polyfill&quot;</span>,path.resolve(__dirname,<span class="hljs-string">&#x27;../src/index.js&#x27;</span>)],    <span class="hljs-comment">// 入口文件</span><br>&#125;<br></code></pre></td></tr></table></figure>

<h2 id="2-搭建-vue-开发环境"><a href="#2-搭建-vue-开发环境" class="headerlink" title="2 搭建 vue 开发环境"></a>2 搭建 vue 开发环境</h2><p>上面的小例子已经帮助而我们实现了打包 css、图片、js、html 等文件。 但是我们还需要以下几种配置</p>
<h4 id="2-1-解析-vue-文件"><a href="#2-1-解析-vue-文件" class="headerlink" title="2.1 解析.vue 文件"></a>2.1 解析.vue 文件</h4><figure class="highlight coffeescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs coffeescript"><span class="hljs-built_in">npm</span> i -D vue-loader vue-template-compiler vue-style-loader<br><span class="hljs-built_in">npm</span> i -S vue<br></code></pre></td></tr></table></figure>

<p><code>vue-loader</code> 用于解析<code>.vue</code>文件<br><code>vue-template-compiler</code> 用于编译模板 配置如下</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">const</span> vueLoaderPlugin = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;vue-loader/lib/plugin&#x27;</span>)<br><span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span> = &#123;<br>    <span class="hljs-attr">module</span>:&#123;<br>        <span class="hljs-attr">rules</span>:[&#123;<br>            <span class="hljs-attr">test</span>:<span class="hljs-regexp">/\.vue$/</span>,<br>            <span class="hljs-attr">use</span>:[<span class="hljs-string">&#x27;vue-loader&#x27;</span>]<br>        &#125;,]<br>     &#125;,<br>    <span class="hljs-attr">resolve</span>:&#123;<br>        <span class="hljs-attr">alias</span>:&#123;<br>          <span class="hljs-string">&#x27;vue$&#x27;</span>:<span class="hljs-string">&#x27;vue/dist/vue.runtime.esm.js&#x27;</span>,<br>          <span class="hljs-string">&#x27; @&#x27;</span>:path.<span class="hljs-title function_">resolve</span>(__dirname,<span class="hljs-string">&#x27;../src&#x27;</span>)<br>        &#125;,<br>        <span class="hljs-attr">extensions</span>:[<span class="hljs-string">&#x27;*&#x27;</span>,<span class="hljs-string">&#x27;.js&#x27;</span>,<span class="hljs-string">&#x27;.json&#x27;</span>,<span class="hljs-string">&#x27;.vue&#x27;</span>]<br>   &#125;,<br>   <span class="hljs-attr">plugins</span>:[<br>        <span class="hljs-keyword">new</span> <span class="hljs-title function_">vueLoaderPlugin</span>()<br>   ]<br>&#125;<br></code></pre></td></tr></table></figure>

<h4 id="2-2-配置-webpack-dev-server-进行热更新"><a href="#2-2-配置-webpack-dev-server-进行热更新" class="headerlink" title="2.2 配置 webpack-dev-server 进行热更新"></a>2.2 配置 webpack-dev-server 进行热更新</h4><figure class="highlight axapta"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs axapta">npm i -D webpack-dev-<span class="hljs-keyword">server</span><br></code></pre></td></tr></table></figure>

<p>配置如下</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-type">const</span> <span class="hljs-variable">Webpack</span> <span class="hljs-operator">=</span> require(<span class="hljs-string">&#x27;webpack&#x27;</span>)<br><span class="hljs-keyword">module</span>.<span class="hljs-keyword">exports</span> = &#123;<br>  <span class="hljs-comment">// ...省略其他配置</span><br>  devServer:&#123;<br>    port:<span class="hljs-number">3000</span>,<br>    hot:<span class="hljs-literal">true</span>,<br>    contentBase:<span class="hljs-string">&#x27;../dist&#x27;</span><br>  &#125;,<br>  plugins:[<br>    <span class="hljs-keyword">new</span> <span class="hljs-title class_">Webpack</span>.HotModuleReplacementPlugin()<br>  ]<br>&#125;<br><br></code></pre></td></tr></table></figure>

<p>完整配置如下</p>
<figure class="highlight awk"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br></pre></td><td class="code"><pre><code class="hljs awk"><span class="hljs-regexp">//</span> webpack.config.js<br><br>const path = require(<span class="hljs-string">&#x27;path&#x27;</span>);<br>const HtmlWebpackPlugin = require(<span class="hljs-string">&#x27;html-webpack-plugin&#x27;</span>)<br>const &#123;CleanWebpackPlugin&#125; = require(<span class="hljs-string">&#x27;clean-webpack-plugin&#x27;</span>)<br>const vueLoaderPlugin = require(<span class="hljs-string">&#x27;vue-loader/lib/plugin&#x27;</span>)<br>const Webpack = require(<span class="hljs-string">&#x27;webpack&#x27;</span>)<br><br><br>module.exports = &#123;<br>    mode:<span class="hljs-string">&#x27;development&#x27;</span>, <span class="hljs-regexp">//</span> 开发模式<br>    entry: &#123;<br>        main:path.resolve(__dirname,<span class="hljs-string">&#x27;./src/main.js&#x27;</span>),<br>        <span class="hljs-regexp">//</span> header:path.resolve(__dirname,<span class="hljs-string">&#x27;./src/header.js&#x27;</span>)<br>    &#125;,    <span class="hljs-regexp">//</span> 入口文件<br>    output: &#123;<br>        filename: <span class="hljs-string">&#x27;[name].[hash:8].js&#x27;</span>,      <span class="hljs-regexp">//</span> 打包后的文件名称<br>        path: path.resolve(__dirname,<span class="hljs-string">&#x27;./dist&#x27;</span>)  <span class="hljs-regexp">//</span> 打包后的目录<br>    &#125;,<br>    devServer:&#123;<br>        port:<span class="hljs-number">3000</span>,<br>        hot:true,<br>    &#125;,<br>    plugins:[<br>        <span class="hljs-regexp">//</span> new HtmlWebpackPlugin(&#123;<br>        <span class="hljs-regexp">//</span>     filename:<span class="hljs-string">&#x27;index.html&#x27;</span>,<br>        <span class="hljs-regexp">//</span>     chunks:[<span class="hljs-string">&#x27;main&#x27;</span>] <span class="hljs-regexp">//</span> 与入口文件对应的模块名<br>        <span class="hljs-regexp">//</span> &#125;),<br>        <span class="hljs-regexp">//</span> new HtmlWebpackPlugin(&#123;<br>        <span class="hljs-regexp">//</span>     filename:<span class="hljs-string">&#x27;header.html&#x27;</span>,<br>        <span class="hljs-regexp">//</span>     chunks:[<span class="hljs-string">&#x27;header&#x27;</span>] <span class="hljs-regexp">//</span> 与入口文件对应的模块名<br>        <span class="hljs-regexp">//</span> &#125;),<br>        new HtmlWebpackPlugin(&#123;<br>          template:path.resolve(__dirname,<span class="hljs-string">&#x27;./public/index.html&#x27;</span>),<br>          filename:<span class="hljs-string">&#x27;index.html&#x27;</span><br>        &#125;),<br>        new CleanWebpackPlugin(),<br>        new vueLoaderPlugin(),<br>        new Webpack.HotModuleReplacementPlugin()<br>    ],<br>    module:&#123;<br>        rules:[<br>            &#123;<br>            test:<span class="hljs-regexp">/\.css$/</span>,<br>            use:[<span class="hljs-string">&#x27;style-loader&#x27;</span>,<span class="hljs-string">&#x27;css-loader&#x27;</span>] <span class="hljs-regexp">//</span> 从右向左解析原则<br>            &#125;,<br>            &#123;<br>                test: <span class="hljs-regexp">/\.scss$/</span>,<br>                use:[ <span class="hljs-string">&#x27;style-loader&#x27;</span>,<span class="hljs-string">&#x27;css-loader&#x27;</span>,<span class="hljs-string">&#x27;postcss-loader&#x27;</span>, <span class="hljs-string">&#x27;sass-loader&#x27;</span>],<br>            &#125;,<br>            &#123;<br>                test: <span class="hljs-regexp">/\.(jpe?g|png|gif)$/i</span>, <span class="hljs-regexp">//</span>图片文件<br>                use: [<br>                  &#123;<br>                    loader: <span class="hljs-string">&#x27;url-loader&#x27;</span>,<br>                    options: &#123;<br>                      limit: <span class="hljs-number">10240</span>,<br>                      fallback: &#123;<br>                        loader: <span class="hljs-string">&#x27;file-loader&#x27;</span>,<br>                        options: &#123;<br>                            name: <span class="hljs-string">&#x27;img/[name].[hash:8].[ext]&#x27;</span><br>                        &#125;<br>                      &#125;<br>                    &#125;<br>                  &#125;<br>                ]<br>            &#125;,<br>            &#123;<br>                test: <span class="hljs-regexp">/\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/</span>, <span class="hljs-regexp">//</span>媒体文件<br>                use: [<br>                  &#123;<br>                    loader: <span class="hljs-string">&#x27;url-loader&#x27;</span>,<br>                    options: &#123;<br>                      limit: <span class="hljs-number">10240</span>,<br>                      fallback: &#123;<br>                        loader: <span class="hljs-string">&#x27;file-loader&#x27;</span>,<br>                        options: &#123;<br>                          name: <span class="hljs-string">&#x27;media/[name].[hash:8].[ext]&#x27;</span><br>                        &#125;<br>                      &#125;<br>                    &#125;<br>                  &#125;<br>                ]<br>            &#125;,<br>            &#123;<br>                test: <span class="hljs-regexp">/\.(woff2?|eot|ttf|otf)(\?.*)?$/i</span>, <span class="hljs-regexp">//</span> 字体<br>                use: [<br>                  &#123;<br>                    loader: <span class="hljs-string">&#x27;url-loader&#x27;</span>,<br>                    options: &#123;<br>                      limit: <span class="hljs-number">10240</span>,<br>                      fallback: &#123;<br>                        loader: <span class="hljs-string">&#x27;file-loader&#x27;</span>,<br>                        options: &#123;<br>                          name: <span class="hljs-string">&#x27;fonts/[name].[hash:8].[ext]&#x27;</span><br>                        &#125;<br>                      &#125;<br>                    &#125;<br>                  &#125;<br>                ]<br>            &#125;,<br>            &#123;<br>                test:<span class="hljs-regexp">/\.js$/</span>,<br>                use:&#123;<br>                  loader:<span class="hljs-string">&#x27;babel-loader&#x27;</span>,<br>                  options:&#123;<br>                    presets:[<span class="hljs-string">&#x27;@babel/preset-env&#x27;</span>]<br>                  &#125;<br>                &#125;,<br>                exclude:<span class="hljs-regexp">/node_modules/</span><br>            &#125;,<br>            &#123;<br>                test: <span class="hljs-regexp">/\.vue$/</span>,<br>                use: [<span class="hljs-string">&#x27;vue-loader&#x27;</span>]<br>            &#125;,<br>        ]<br>    &#125;,<br>    resolve:&#123;<br>        alias:&#123;<br>          <span class="hljs-string">&#x27;vue$&#x27;</span>:<span class="hljs-string">&#x27;vue/dist/vue.runtime.esm.js&#x27;</span>,<br>          <span class="hljs-string">&#x27; @&#x27;</span>:path.resolve(__dirname,<span class="hljs-string">&#x27;./src&#x27;</span>)<br>        &#125;,<br>        extensions:[<span class="hljs-string">&#x27;*&#x27;</span>,<span class="hljs-string">&#x27;.js&#x27;</span>,<span class="hljs-string">&#x27;.json&#x27;</span>,<span class="hljs-string">&#x27;.vue&#x27;</span>]<br>   &#125;,<br>&#125;<br><br></code></pre></td></tr></table></figure>

<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><code class="hljs php"><span class="hljs-comment">// webpack.dev.js</span><br><br><span class="hljs-keyword">const</span> <span class="hljs-variable constant_">Webpack</span> = <span class="hljs-keyword">require</span>(<span class="hljs-string">&#x27;webpack&#x27;</span>)<br><span class="hljs-keyword">const</span> <span class="hljs-variable constant_">webpackConfig</span> = <span class="hljs-keyword">require</span>(<span class="hljs-string">&#x27;./webpack.config.js&#x27;</span>)<br><span class="hljs-keyword">const</span> <span class="hljs-variable constant_">WebpackMerge</span> = <span class="hljs-keyword">require</span>(<span class="hljs-string">&#x27;webpack-merge&#x27;</span>)<br>module.exports = <span class="hljs-title function_ invoke__">WebpackMerge</span>(webpackConfig,&#123;<br>  <span class="hljs-attr">mode</span>:<span class="hljs-string">&#x27;development&#x27;</span>,<br>  <span class="hljs-attr">devtool</span>:<span class="hljs-string">&#x27;cheap-module-eval-source-map&#x27;</span>,<br>  <span class="hljs-attr">devServer</span>:&#123;<br>    <span class="hljs-attr">port</span>:<span class="hljs-number">3000</span>,<br>    <span class="hljs-attr">hot</span>:<span class="hljs-literal">true</span>,<br>  &#125;,<br>  <span class="hljs-attr">plugins</span>:[<br>    <span class="hljs-keyword">new</span> <span class="hljs-title class_">Webpack</span>.<span class="hljs-title function_ invoke__">HotModuleReplacementPlugin</span>()<br>  ]<br>&#125;)<br><br></code></pre></td></tr></table></figure>

<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><code class="hljs php"><span class="hljs-comment">// webpack.prod.js</span><br><br><span class="hljs-keyword">const</span> <span class="hljs-variable constant_">path</span> = <span class="hljs-keyword">require</span>(<span class="hljs-string">&#x27;path&#x27;</span>)<br><span class="hljs-keyword">const</span> <span class="hljs-variable constant_">webpackConfig</span> = <span class="hljs-keyword">require</span>(<span class="hljs-string">&#x27;./webpack.config.js&#x27;</span>)<br><span class="hljs-keyword">const</span> <span class="hljs-variable constant_">WebpackMerge</span> = <span class="hljs-keyword">require</span>(<span class="hljs-string">&#x27;webpack-merge&#x27;</span>)<br><span class="hljs-keyword">const</span> <span class="hljs-variable constant_">CopyWebpackPlugin</span> = <span class="hljs-keyword">require</span>(<span class="hljs-string">&#x27;copy-webpack-plugin&#x27;</span>)<br><span class="hljs-keyword">const</span> <span class="hljs-variable constant_">OptimizeCssAssetsPlugin</span> = <span class="hljs-keyword">require</span>(<span class="hljs-string">&#x27;optimize-css-assets-webpack-plugin&#x27;</span>)<br><span class="hljs-keyword">const</span> <span class="hljs-variable constant_">UglifyJsPlugin</span> = <span class="hljs-keyword">require</span>(<span class="hljs-string">&#x27;uglifyjs-webpack-plugin&#x27;</span>)<br>module.exports = <span class="hljs-title function_ invoke__">WebpackMerge</span>(webpackConfig,&#123;<br>  <span class="hljs-attr">mode</span>:<span class="hljs-string">&#x27;production&#x27;</span>,<br>  <span class="hljs-attr">devtool</span>:<span class="hljs-string">&#x27;cheap-module-source-map&#x27;</span>,<br>  <span class="hljs-attr">plugins</span>:[<br>    <span class="hljs-keyword">new</span> <span class="hljs-title class_">CopyWebpackPlugin</span>([&#123;<br>      <span class="hljs-attr">from</span>:path.<span class="hljs-title function_ invoke__">resolve</span>(__dirname,<span class="hljs-string">&#x27;./public&#x27;</span>),<br>      <span class="hljs-attr">to</span>:path.<span class="hljs-title function_ invoke__">resolve</span>(__dirname,<span class="hljs-string">&#x27;./dist&#x27;</span>)<br>    &#125;]),<br>  ],<br>  optimization:&#123;<br>    minimizer:[<br>      <span class="hljs-keyword">new</span> <span class="hljs-title class_">UglifyJsPlugin</span>(&#123;<span class="hljs-comment">//压缩js</span><br>        cache:<span class="hljs-literal">true</span>,<br>        parallel:<span class="hljs-literal">true</span>,<br>        sourceMap:<span class="hljs-literal">true</span><br>    &#125;),<br>    <span class="hljs-keyword">new</span> <span class="hljs-title class_">OptimizeCssAssetsPlugin</span>(&#123;&#125;)<br>    ],<br>    splitChunks:&#123;<br>      chunks:<span class="hljs-string">&#x27;all&#x27;</span>,<br>      cacheGroups:&#123;<br>        libs: &#123;<br>          name: <span class="hljs-string">&quot;chunk-libs&quot;</span>,<br>          test: /[\\/]node_modules[\\/]/,<br>          priority: <span class="hljs-number">10</span>,<br>          chunks: <span class="hljs-string">&quot;initial&quot;</span> <span class="hljs-comment">// 只打包初始时依赖的第三方</span><br>        &#125;<br>      &#125;<br>    &#125;<br>  &#125;<br>&#125;)<br><br></code></pre></td></tr></table></figure>

<h3 id="2-4-区分开发环境与生产环境"><a href="#2-4-区分开发环境与生产环境" class="headerlink" title="2.4 区分开发环境与生产环境"></a>2.4 区分开发环境与生产环境</h3><p>实际应用到项目中，我们需要区分开发环境与生产环境，我们在原来 webpack.config.js 的基础上再新增两个文件</p>
<ul>
<li><p><code>webpack.dev.js</code> 开发环境配置文件</p>
<ul>
<li>开发环境主要实现的是热更新,不要压缩代码，完整的 sourceMap</li>
</ul>
</li>
<li><p><code>webpack.prod.js</code> 生产环境配置文件</p>
<ul>
<li>生产环境主要实现的是压缩代码、提取 css 文件、合理的 sourceMap、分割代码<br>需要安装以下模块:<br>npm i -D webpack-merge copy-webpack-plugin optimize-css-assets-webpack-plugin uglifyjs-webpack-plugin</li>
</ul>
</li>
<li><p><code>webpack-merge</code> 合并配置</p>
</li>
<li><p><code>copy-webpack-plugin</code> 拷贝静态资源</p>
</li>
<li><p><code>optimize-css-assets-webpack-plugin</code> 压缩 css</p>
</li>
<li><p><code>uglifyjs-webpack-plugin</code> 压缩 js</p>
</li>
</ul>
<h2 id="2-5-优化-webpack-配置"><a href="#2-5-优化-webpack-配置" class="headerlink" title="2.5 优化 webpack 配置"></a>2.5 优化 webpack 配置</h2><p>优化配置对我们来说非常有实际意义，这实际关系到你打包出来文件的大小，打包的速度等。 具体优化可以分为以下几点：</p>
<h3 id="2-5-1-优化打包速度"><a href="#2-5-1-优化打包速度" class="headerlink" title="2.5.1 优化打包速度"></a>2.5.1 优化打包速度</h3><blockquote>
<p>构建速度指的是我们每次修改代码后热更新的速度以及发布前打包文件的速度。</p>
</blockquote>
<h4 id="2-5-1-2-缩小文件的搜索范围-配置-include-exclude-alias-noParse-extensions"><a href="#2-5-1-2-缩小文件的搜索范围-配置-include-exclude-alias-noParse-extensions" class="headerlink" title="2.5.1.2 缩小文件的搜索范围(配置 include exclude alias noParse extensions)"></a>2.5.1.2 缩小文件的搜索范围(配置 include exclude alias noParse extensions)</h4><ul>
<li><code>alias</code>: 当我们代码中出现 <code>import &#39;vue&#39;</code>时， webpack 会采用向上递归搜索的方式去<code>node_modules</code> 目录下找。为了减少搜索范围我们可以直接告诉 webpack 去哪个路径下查找。也就是别名(<code>alias</code>)的配置。</li>
<li><code>include exclude</code> 同样配置<code>include exclude</code>也可以减少<code>webpack loader</code>的搜索转换时间。</li>
<li><code>noParse </code> 当我们代码中使用到<code>import jq from &#39;jquery&#39;</code>时，<code>webpack</code>会去解析 jq 这个库是否有依赖其他的包。但是我们对类似<code>jquery</code>这类依赖库，一般会认为不会引用其他的包(特殊除外,自行判断)。增加<code>noParse</code>属性,告诉<code>webpack</code>不必解析，以此增加打包速度。</li>
<li><code>extensions ``webpack</code>会根据<code>extensions</code>定义的后缀查找文件(频率较高的文件类型优先写在前面)</li>
</ul>
<h4 id="2-5-1-3-使用-HappyPack-开启多进程-Loader-转换"><a href="#2-5-1-3-使用-HappyPack-开启多进程-Loader-转换" class="headerlink" title="2.5.1.3 使用 HappyPack 开启多进程 Loader 转换"></a>2.5.1.3 使用 HappyPack 开启多进程 Loader 转换</h4><blockquote>
<p>在 webpack 构建过程中，实际上耗费时间大多数用在 loader 解析转换以及代码的压缩中。日常开发中我们需要使用 Loader 对 js，css，图片，字体等文件做转换操作，并且转换的文件数据量也是非常大。由于 js 单线程的特性使得这些转换操作不能并发处理文件，而是需要一个个文件进行处理。HappyPack 的基本原理是将这部分任务分解到多个子进程中去并行处理，子进程处理完成后把结果发送到主进程中，从而减少总的构建时间</p>
</blockquote>
<h4 id="2-5-1-4-使用-webpack-parallel-uglify-plugin-增强代码压缩"><a href="#2-5-1-4-使用-webpack-parallel-uglify-plugin-增强代码压缩" class="headerlink" title="2.5.1.4 使用 webpack-parallel-uglify-plugin 增强代码压缩"></a>2.5.1.4 使用 webpack-parallel-uglify-plugin 增强代码压缩</h4><blockquote>
<p>上面对于 loader 转换已经做优化，那么下面还有另一个难点就是优化代码的压缩时间</p>
</blockquote>
<h4 id="2-5-1-5-抽离第三方模块"><a href="#2-5-1-5-抽离第三方模块" class="headerlink" title="2.5.1.5 抽离第三方模块"></a>2.5.1.5 抽离第三方模块</h4><blockquote>
<p>对于开发项目中不经常会变更的静态依赖文件。类似于我们的<code>elementUi、vue</code>全家桶等等。因为很少会变更，所以我们不希望这些依赖要被集成到每一次的构建逻辑中去。 这样做的好处是每次更改我本地代码的文件的时候，<code>webpack</code>只需要打包我项目本身的文件代码，而不会再去编译第三方库。以后只要我们不升级第三方包的时候，那么<code>webpack</code>就不会对这些库去打包，这样可以快速的提高打包的速度。</p>
</blockquote>
<p>这里我们使用<code>webpack</code>内置的<code>DllPlugin DllReferencePlugin</code>进行抽离<br>在与<code>webpack</code>配置文件同级目录下新建<code>webpack.dll.config.js</code> 代码如下</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><code class="hljs php"><span class="hljs-comment">// webpack.dll.config.js</span><br><span class="hljs-keyword">const</span> <span class="hljs-variable constant_">path</span> = <span class="hljs-keyword">require</span>(<span class="hljs-string">&quot;path&quot;</span>);<br><span class="hljs-keyword">const</span> <span class="hljs-variable constant_">webpack</span> = <span class="hljs-keyword">require</span>(<span class="hljs-string">&quot;webpack&quot;</span>);<br>module.exports = &#123;<br>  <span class="hljs-comment">// 你想要打包的模块的数组</span><br>  entry: &#123;<br>    vendor: [<span class="hljs-string">&#x27;vue&#x27;</span>,<span class="hljs-string">&#x27;element-ui&#x27;</span>]<br>  &#125;,<br>  output: &#123;<br>    path: path.<span class="hljs-title function_ invoke__">resolve</span>(__dirname, <span class="hljs-string">&#x27;static/js&#x27;</span>), <span class="hljs-comment">// 打包后文件输出的位置</span><br>    filename: <span class="hljs-string">&#x27;[name].dll.js&#x27;</span>,<br>    library: <span class="hljs-string">&#x27;[name]_library&#x27;</span><br>     <span class="hljs-comment">// 这里需要和webpack.DllPlugin中的`name: &#x27;[name]_library&#x27;,`保持一致。</span><br>  &#125;,<br>  plugins: [<br>    <span class="hljs-keyword">new</span> webpack.<span class="hljs-title function_ invoke__">DllPlugin</span>(&#123;<br>      <span class="hljs-attr">path</span>: path.<span class="hljs-title function_ invoke__">resolve</span>(__dirname, <span class="hljs-string">&#x27;[name]-manifest.json&#x27;</span>),<br>      <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;[name]_library&#x27;</span>,<br>      <span class="hljs-attr">context</span>: __dirname<br>    &#125;)<br>  ]<br>&#125;;<br></code></pre></td></tr></table></figure>

<p>在<code>package.json</code>中配置如下命令</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs json"><span class="hljs-attr">&quot;dll&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;webpack --config build/webpack.dll.config.js&quot;</span><br></code></pre></td></tr></table></figure>

<p>接下来在我们的<code>webpack.config.js</code>中增加以下代码</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs php">module.exports = &#123;<br>  plugins: [<br>    <span class="hljs-keyword">new</span> webpack.<span class="hljs-title function_ invoke__">DllReferencePlugin</span>(&#123;<br>      <span class="hljs-attr">context</span>: __dirname,<br>      <span class="hljs-attr">manifest</span>: <span class="hljs-keyword">require</span>(<span class="hljs-string">&#x27;./vendor-manifest.json&#x27;</span>)<br>    &#125;),<br>    <span class="hljs-keyword">new</span> <span class="hljs-title class_">CopyWebpackPlugin</span>([ <span class="hljs-comment">// 拷贝生成的文件到dist目录 这样每次不必手动去cv</span><br>      &#123;<span class="hljs-keyword">from</span>: <span class="hljs-string">&#x27;static&#x27;</span>, to:<span class="hljs-string">&#x27;static&#x27;</span>&#125;<br>    ]),<br>  ]<br>&#125;;<br></code></pre></td></tr></table></figure>

<p>执行</p>
<figure class="highlight routeros"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs routeros">npm <span class="hljs-built_in">run</span> dll<br></code></pre></td></tr></table></figure>

<p>会发现生成了我们需要的集合第三地方 代码的<code>vendor.dll.js</code> 我们需要在<code>html</code>文件中手动引入这个<code>js</code>文件</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-keyword">html</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">&quot;en&quot;</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">&quot;UTF-8&quot;</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;viewport&quot;</span> <span class="hljs-attr">content</span>=<span class="hljs-string">&quot;width=device-width, initial-scale=1.0&quot;</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">http-equiv</span>=<span class="hljs-string">&quot;X-UA-Compatible&quot;</span> <span class="hljs-attr">content</span>=<span class="hljs-string">&quot;ie=edge&quot;</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>老yuan<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">&quot;static/js/vendor.dll.js&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;app&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p>这样如果我们没有更新第三方依赖包，就不必<code>npm run dll</code>。直接执行<code>npm run dev npm run build</code>的时候会发现我们的打包速度明显有所提升。因为我们已经通过<code>dllPlugin</code>将第三方依赖包抽离出来了</p>
<h4 id="2-5-1-6-配置缓存"><a href="#2-5-1-6-配置缓存" class="headerlink" title="2.5.1.6 配置缓存"></a>2.5.1.6 配置缓存</h4><blockquote>
<p>我们每次执行构建都会把所有的文件都重复编译一遍，这样的重复工作是否可以被缓存下来呢，答案是可以的，目前大部分 <code>loader</code> 都提供了<code>cache</code> 配置项。比如在 <code>babel-loader</code> 中，可以通过设置<code>cacheDirectory</code> 来开启缓存，<code>babel-loader?cacheDirectory=true</code> 就会将每次的编译结果写进硬盘文件（默认是在项目根目录下的<code>node_modules/.cache/babel-loader</code>目录内，当然你也可以自定义）</p>
</blockquote>
<p>但如果 <code>loader</code> 不支持缓存呢？我们也有方法,我们可以通过<code>cache-loader</code> ，它所做的事情很简单，就是 <code>babel-loader</code> 开启 <code>cache </code>后做的事情，将 <code>loader</code> 的编译结果写入硬盘缓存。再次构建会先比较一下，如果文件较之前的没有发生变化则会直接使用缓存。使用方法如官方 demo 所示，在一些性能开销较大的 loader 之前添加此 loader 即可</p>
<figure class="highlight coffeescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs coffeescript"><span class="hljs-built_in">npm</span> i -D cache-loader<br></code></pre></td></tr></table></figure>

<p><img src="https://image.lxandzjy.top/2021/11/17/eccbd354b1c97.png" srcset="/img/loading.gif" lazyload></p>
<h3 id="2-5-2-优化打包文件体积"><a href="#2-5-2-优化打包文件体积" class="headerlink" title="2.5.2 优化打包文件体积"></a>2.5.2 优化打包文件体积</h3><blockquote>
<p>打包的速度我们是进行了优化，但是打包后的文件体积却是十分大，造成了页面加载缓慢，浪费流量等，接下来让我们从文件体积上继续优化</p>
</blockquote>
<h4 id="2-5-2-1-引入-webpack-bundle-analyzer-分析打包后的文件"><a href="#2-5-2-1-引入-webpack-bundle-analyzer-分析打包后的文件" class="headerlink" title="2.5.2.1 引入 webpack-bundle-analyzer 分析打包后的文件"></a>2.5.2.1 引入 webpack-bundle-analyzer 分析打包后的文件</h4><p><code>webpack-bundle-analyzer</code>将打包后的内容束展示为方便交互的直观树状图，让我们知道我们所构建包中真正引入的内容</p>
<figure class="highlight coffeescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs coffeescript"><span class="hljs-built_in">npm</span> i -D webpack-bundle-analyzer<br></code></pre></td></tr></table></figure>

<p>接下来在<code>package.json</code>里配置启动命令</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs json"><span class="hljs-attr">&quot;analyz&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;NODE_ENV=production npm_config_report=true npm run build&quot;</span><br></code></pre></td></tr></table></figure>

<p>windows 请安装<code>npm i -D cross-env</code></p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs json"><span class="hljs-attr">&quot;analyz&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;cross-env NODE_ENV=production npm_config_report=true npm run build&quot;</span><br></code></pre></td></tr></table></figure>

<h4 id="2-5-2-3-externals"><a href="#2-5-2-3-externals" class="headerlink" title="2.5.2.3 externals"></a>2.5.2.3 externals</h4><blockquote>
<p>按照官方文档的解释，如果我们想引用一个库，但是又不想让<code>webpack</code>打包，并且又不影响我们在程序中以<code>CMD、AMD</code>或者<code>window/global</code>全局等方式进行使用，那就可以通过配置<code>Externals</code>。这个功能主要是用在创建一个库的时候用的，但是也可以在我们项目开发中充分使用 <code>Externals</code>的方式，我们将这些不需要打包的静态资源从构建逻辑中剔除出去，而使用 <code>CDN</code> 的方式，去引用它们。</p>
</blockquote>
<p>有时我们希望我们通过<code>script</code>引入的库，如用 CDN 的方式引入的<code>jquery</code>，我们在使用时，依旧用<code>require</code>的方式来使用，但是却不希望<code>webpack</code>将它又编译进文件中。这里官网案例已经足够清晰明了，大家有兴趣可以点击了解</p>
<p><a href="https://link.juejin.cn/?target=https://webpack.js.org/configuration/externals/%23root">webpack</a> 官网案例如下</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs javascript">&lt;script<br>  src=<span class="hljs-string">&quot;https://code.jquery.com/jquery-3.1.0.js&quot;</span><br>  integrity=<span class="hljs-string">&quot;sha256-slogkvB1K3VOkzAI8QITxV3VzpOnkeNVsKvtkYLMjfk=&quot;</span><br>  crossorigin=<span class="hljs-string">&quot;anonymous&quot;</span>&gt;<br>&lt;/script&gt;<br><br><span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span> = &#123;<br>  <span class="hljs-comment">//...</span><br>  <span class="hljs-attr">externals</span>: &#123;<br>    <span class="hljs-attr">jquery</span>: <span class="hljs-string">&#x27;jQuery&#x27;</span><br>  &#125;<br>&#125;;<br>复制代码<br><span class="hljs-keyword">import</span> $ <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;jquery&#x27;</span>;<br>$(<span class="hljs-string">&#x27;.my-element&#x27;</span>).<span class="hljs-title function_">animate</span>(<span class="hljs-comment">/* ... */</span>);<br></code></pre></td></tr></table></figure>

<blockquote>
<p><code>loader</code>从本质上来说其实就是一个<code>node</code>模块。相当于一台榨汁机<code>(loader)</code>将相关类型的文件代码<code>(code)</code>给它。根据我们设置的规则，经过它的一系列加工后还给我们加工好的果汁<code>(code)</code>。</p>
</blockquote>
<blockquote>
<p>知识点普及之<code>AST</code>。<code>AST</code>通俗的来说，假设我们有一个文件<code>a.js</code>,我们对<code>a.js</code>里面的 1000 行进行一些操作处理,比如为所有的<code>await</code> 增加<code>try catch</code>,以及其他操作，但是<code>a.js</code>里面的代码本质上来说就是一堆字符串。那我们怎么办呢，那就是转换为带标记信息的对象(抽象语法树)我们方便进行增删改查。这个带标记的对象(抽象语法树)就是<code>AST</code>。这里推荐一篇不错的 AST 文章 <a href="https://link.juejin.cn/?target=https://segmentfault.com/a/1190000016231512">AST 快速入门</a></p>
</blockquote>
<blockquote>
<p>在 <code>Webpack</code> 运行的生命周期中会广播出许多事件，<code>Plugin</code> 可以监听这些事件，在合适的时机通过<code>Webpack</code>提供的<code>API</code>改变输出结果。通俗来说：一盘美味的 <a href="https://link.juejin.cn/?target=https://www.meishij.net/zuofa/yandouchaojidan.html">盐豆炒鸡蛋</a> 需要经历烧油 炒制 调味到最后的装盘等过程，而<code>plugin</code>相当于可以监控每个环节并进行操作，比如可以写一个少放胡椒粉<code>plugin</code>,监控<code>webpack</code>暴露出的生命周期事件(调味)，在调味的时候执行少放胡椒粉操作。那么它与<code>loader</code>的区别是什么呢？上面我们也提到了<code>loader</code>的单一原则,<code>loader</code>只能一件事，比如说<code>less-loader</code>,只能解析<code>less</code>文件，<code>plugin</code>则是针对整个流程执行广泛的任务。</p>
</blockquote>
<p>动手实践学习于 <a target="_blank" rel="noopener" href="https://juejin.cn/post/6844904031240863758">https://juejin.cn/post/6844904031240863758</a> ,来源：稀土掘金</p>

              
            </div>
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" class="category-chain-item">学习笔记</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/webpack/">#webpack</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>webpack实战</div>
      <div>https://zjy365.github.io/2021/11/10/webpack实战/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>zjy</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2021年11月10日</div>
        </div>
      
      
      <div class="license-meta-item">
        <div>许可协议</div>
        <div>
          
            
            
              <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
              <span class="hint--top hint--rounded" aria-label="BY - 署名">
                <i class="iconfont icon-by"></i>
              </span>
              </a>
            
          
        </div>
      </div>
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2021/11/10/this%E3%80%81apply%E3%80%81call%E3%80%81bind/" title="this、apply、call、bind的理解">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">this、apply、call、bind的理解</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2021/10/25/css%E5%8A%A8%E7%94%BB/" title="CSS动画">
                        <span class="hidden-mobile">CSS动画</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  






    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
  
    <!-- 备案信息 ICP for China -->
    <div class="beian">
  <span>
    <a href="http://beian.miit.gov.cn/" target="_blank" rel="nofollow noopener">
      豫ICP备2021008845号-1
    </a>
  </span>
  
</div>

  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>






  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      headingSelector : CONFIG.toc.headingSelector || 'h1,h2,h3,h4,h5,h6',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      collapseDepth   : CONFIG.toc.collapseDepth || 0,
      scrollSmooth    : true,
      headingsOffset  : -boardTop
    });
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }
  });
</script>


  <script>
  (function() {
    var enableLang = CONFIG.code_language.enable && CONFIG.code_language.default;
    var enableCopy = CONFIG.copy_btn;
    if (!enableLang && !enableCopy) {
      return;
    }

    function getBgClass(ele) {
      return Fluid.utils.getBackgroundLightness(ele) >= 0 ? 'code-widget-light' : 'code-widget-dark';
    }

    var copyTmpl = '';
    copyTmpl += '<div class="code-widget">';
    copyTmpl += 'LANG';
    copyTmpl += '</div>';
    jQuery('.markdown-body pre').each(function() {
      var $pre = jQuery(this);
      if ($pre.find('code.mermaid').length > 0) {
        return;
      }
      if ($pre.find('span.line').length > 0) {
        return;
      }

      var lang = '';

      if (enableLang) {
        lang = CONFIG.code_language.default;
        if ($pre[0].children.length > 0 && $pre[0].children[0].classList.length >= 2 && $pre.children().hasClass('hljs')) {
          lang = $pre[0].children[0].classList[1];
        } else if ($pre[0].getAttribute('data-language')) {
          lang = $pre[0].getAttribute('data-language');
        } else if ($pre.parent().hasClass('sourceCode') && $pre[0].children.length > 0 && $pre[0].children[0].classList.length >= 2) {
          lang = $pre[0].children[0].classList[1];
          $pre.parent().addClass('code-wrapper');
        } else if ($pre.parent().hasClass('markdown-body') && $pre[0].classList.length === 0) {
          $pre.wrap('<div class="code-wrapper"></div>');
        }
        lang = lang.toUpperCase().replace('NONE', CONFIG.code_language.default);
      }
      $pre.append(copyTmpl.replace('LANG', lang).replace('code-widget">',
        getBgClass($pre[0]) + (enableCopy ? ' code-widget copy-btn" data-clipboard-snippet><i class="iconfont icon-copy"></i>' : ' code-widget">')));

      if (enableCopy) {
        Fluid.utils.createScript('https://lib.baomitu.com/clipboard.js/2.0.10/clipboard.min.js', function() {
          var clipboard = new window.ClipboardJS('.copy-btn', {
            target: function(trigger) {
              var nodes = trigger.parentNode.childNodes;
              for (var i = 0; i < nodes.length; i++) {
                if (nodes[i].tagName === 'CODE') {
                  return nodes[i];
                }
              }
            }
          });
          clipboard.on('success', function(e) {
            e.clearSelection();
            e.trigger.innerHTML = e.trigger.innerHTML.replace('icon-copy', 'icon-success');
            setTimeout(function() {
              e.trigger.innerHTML = e.trigger.innerHTML.replace('icon-success', 'icon-copy');
            }, 2000);
          });
        });
      }
    });
  })();
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
